<template>
  <div>

    <el-form  style="display: flex; justify-content: flex-start; margin: 10px 0" :inline="true"  class="demo-form-inline">
        <el-form-item label="名称">
          <el-input v-model="name" placeholder="名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" circle @click="queryAllFood()"
            ></el-button
          >
        </el-form-item>
      </el-form>
    <el-button  style="display: flex; justify-content: flex-start; margin: 10px 0" type="primary"
    @click="dialogFormVisible = true;food={};sign='add';title1='新增食品'"
    >新增</el-button>
    <el-table :data="foodList" @selection-change="handleSelectionChange" height="65vh">
      <el-table-column prop="foodid" type="selection">
      </el-table-column>
      <el-table-column prop="foodid" label="编号">
      </el-table-column>
      <el-table-column prop="foodname" label="食品名称" >
      </el-table-column>
      <el-table-column prop="foodtype" label="所属类型" >
        <template slot-scope="scope">
            <span>{{ scope.row.foodtype | lx }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="foodheat" label="100g含能量" >
      </el-table-column>
      <el-table-column prop="protein" label="蛋白质含量" >
      </el-table-column>
      <el-table-column prop="fat" label="脂肪含量" width="120">
      </el-table-column>
      <el-table-column prop="foodid" label="操作">
        <template slot-scope="scope">
          <el-button 
          @click="
              dialogFormVisible = true;
              sign = 'update';
             food = scope.row;
             title1='修改食品'
            "
          type="primary" icon="el-icon-edit" circle>
          </el-button>
          <el-button   @click="DeleteFood(scope.row.foodid)" type="danger" icon="el-icon-delete" circle></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[2, 3, 4, 6, 10]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

    <el-dialog :title="title1" :visible.sync="dialogFormVisible">
      <el-form :model="food">
        <el-form-item label="食品名称" :label-width="formLabelWidth">
          <el-input v-model="food.foodname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="食品类型" :label-width="formLabelWidth">
          <el-select v-model="food.foodtype" placeholder="请选择食品类型">
            <el-option label="坚果" :value="0"></el-option>
            <el-option label="水果" :value="1"></el-option>
            <el-option label="热食" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="100g含能量" :label-width="formLabelWidth">
          <el-input v-model="food.foodheat" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="蛋白质含量" :label-width="formLabelWidth">
          <el-input v-model="food.protein" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="脂肪含量" :label-width="formLabelWidth">
          <el-input v-model="food.fat" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="
            dialogFormVisible = false;
            add();
          "
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
        name:"",
        sign:"add",
        food:{},
        title1:"新增食品",
        foodList:[],
      dialogTableVisible: false,
      dialogFormVisible: false,
      currentPage: 1,
      total: 3,
      pageSize: 4,
      formLabelWidth: "120px",
    };
  },
  created() {
    this.queryAllFood();
  },
  filters:{
    lx:function(a){
        return a==0?"坚果":a==1?"水果":"熟食";
    }
  },
  methods: {
    add(){
        if(this.sign=="add"){
  
      this.axios.post("/api/lzh/addFood", this.food).then((result) => {
        this.$message({
          message: "添加成功",
          type: "success",
        });
        this.queryAllFood();
      });
      }else{
      
      this.axios.post("/api/lzh/UpdateFood", this.food).then((result) => {
        this.$message({
          message: "修改成功",
          type: "success",
        });
        this.queryAllFood();
      });
      }
    },
    DeleteFood(data){
            console.log(data)

            this.axios.post("/api/lzh/deleteFood?id="+data).then(resule=>{
              this.$message({
                message: '删除成功！',
                type: 'success'
              });
              this.queryAllFood();
            })
    },
    queryAllFood() {
        this.axios.post("/api/lzh/queryAllFood",{
            pageNum: this.currentPage,
            pageSize: this.pageSize,
            searchObj: {
                foodname: this.name,
            },
        }).then(result=>{
            this.total = result.data.total;
             this.foodList = result.data.list;
        })
    },
    handleSelectionChange(val) {
      // console.log(val)
      this.multipleSelection = val;
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.queryAllFood();
      // console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.queryAllFood();
      // console.log(`当前页: ${val}`);
    },
  },
};
</script>

<style>
</style>